import os

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
import time


class MouseOperation:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://sahitest.com/demo/clicks.htm')
        self.driver.maximize_window()

    def click(self):
        btn = self.driver.find_element(By.XPATH, '//form/input[3]')
        ac = ActionChains(self.driver)
        ac.click(btn).perform()

    def context_click(self):
        ac = ActionChains(self.driver)
        btn = self.driver.find_element(By.XPATH, '//form/input[4]')
        ac.context_click(btn).perform()
        time.sleep(3)

    def double(self):
        time.sleep(2)
        ac = ActionChains(self.driver)
        btn = self.driver.find_element(By.XPATH, '//form/input[2]')
        ac.double_click(btn).perform()
        time.sleep(3)

    def drag_and_drop(self):
        self.driver.get("https://sahitest.com/demo/dragDropMooTools.htm")
        time.sleep(2)
        ac = ActionChains(driver=self.driver)
        source = self.driver.find_element(By.ID, "dragger")
        btn = self.driver.find_element(By.XPATH, "//div[4]")
        ac.drag_and_drop(source, btn).perform()

    def moveseover(self):
        self.driver.get("https://sahitest.com/demo/dragDropMooTools.htm")
        time.sleep(2)
        ac = ActionChains(driver=self.driver)
        source = self.driver.find_element(By.XPATH, "//div/div")
        ac.move_to_element(source).perform()

    def click_and_hold(self):
        url = "file:///" + os.path.abspath("html") + "/mouse_hold.html"
        self.driver.get(url)
        ac = ActionChains(self.driver)
        bt = self.driver.find_element(By.ID, "btn1")
        ac.click_and_hold(bt).perform()

    def quit(self):
        self.driver.quit()


if __name__ == '__main__':
    MOU = MouseOperation()
    # MOU.click()
    # MOU.context_click()
    MOU.click_and_hold()
    # MOU.drag_and_drop()
    # MOU.click_and_hold()

    # MOU.double()
    MOU.quit()
